﻿#include "BrowserTableHandlerSubsystemSemiInfiniteF.h"
#include "OperatorsInterface/OperAbstractSubsystem.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractSEASubSys.h"
#include "FITK_Component/FITKAcousticsModel/FITKSEASubSysSemiInfiniteFluid.h"
#include "FITK_Component/FITKAcousticsModel/FITKSEASubSysGeoProp.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"

#include <QCheckBox>

namespace GUI
{
    void BrowserTableHandlerSubsystemSemiInfiniteF::updateTable()
    {
        if (_tableWidget == nullptr|| _seaMgr==nullptr) return;
        _tableWidget->clear();
        _tableWidget->setColumnCount(0);
        _tableWidget->setRowCount(0);
     
        QList<Acoustics::FITKAbstractSEASubSys*> list = _seaMgr->getSubSysByType(
            Acoustics::FITKAbstractSEASubSys::SEASemiIInfiniteFluid);
        for (int i = 0; i < list.size(); ++i)
        {
            // 转换为SEA半无限流体
            Acoustics::FITKSEASubSysSemiInfiniteFluid* sea = dynamic_cast<Acoustics::FITKSEASubSysSemiInfiniteFluid*>(list.at(i));
            if (sea == nullptr) continue;
            //记录最大层数
            int layers = sea->getDataCount();
            if (layers > _maxlayers)
                _maxlayers = layers;
        }
        //设置表头
        QStringList headerLabels;
        headerLabels << tr("Name") << tr("Enabled") << tr("Fluid");
        for (int i = 0; i < _maxlayers; ++i)
        {
            QString Layer = QString("Connection-%1").arg(i + 1);
            headerLabels << Layer;
        }
        headerLabels << tr("Notes");
        _tableWidget->setColumnCount(headerLabels.size());
        _tableWidget->setHorizontalHeaderLabels(headerLabels);
        //更新表格
        this->updateData();
    }
    void BrowserTableHandlerSubsystemSemiInfiniteF::updateData()
    {
        if (_tableWidget == nullptr) return;
        if (_seaMgr == nullptr || _propMgr == nullptr)return;

        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (!phys)return;
        Acoustics::FITKAcousticsMaterialManager* matMgr = phys->getMaterialManager();
        if (!phys)matMgr;

        QList<Acoustics::FITKAbstractSEASubSys*> list = _seaMgr->getSubSysByType(
            Acoustics::FITKAbstractSEASubSys::SEASemiIInfiniteFluid);

        //物理属性数量
        const int n = list.size();
        _tableWidget->setRowCount(n);

        for (int i = 0; i < n; ++i)
        {
            // 转换为SEA半无限流体
            Acoustics::FITKSEASubSysSemiInfiniteFluid* sea = dynamic_cast<Acoustics::FITKSEASubSysSemiInfiniteFluid*>(list.at(i));
            if (sea == nullptr) continue;

            //数据ID
            const int id = sea->getDataObjectID();

            _tableWidget->setItem(i, 0, new QTableWidgetItem(sea->getDataObjectName(), id));
            QCheckBox* box = new QCheckBox("");
            box->setChecked(sea->getIsEnabled());
            connect(box, &QCheckBox::stateChanged, this, [sea](int state) {
                if (state == Qt::Checked)  sea->setIsEnabled(true); 
                else if (state == Qt::Unchecked)  sea->setIsEnabled(false);
            });
            _tableWidget->setCellWidget(i, 1, box);
            auto matName = matMgr->getDataByID(sea->getMaterialID());
            if (!matName)continue;
            _tableWidget->setItem(i, 2, new QTableWidgetItem(matName->getDataObjectName(), id));
            for (int j = 0; j < sea->getDataCount(); ++j)
            {
                Acoustics::FITKSEASubSysSEIFConnectedSubSys* data = dynamic_cast<Acoustics::FITKSEASubSysSEIFConnectedSubSys*>(sea->getDataByIndex(j));
                if (!data||!data->getSubSysObj())continue;
                _tableWidget->setItem(i, 2+j+1, new QTableWidgetItem(data->getSubSysObj()->getDataObjectName(), id));
            }
            //_tableWidget->setItem(i, 2 + _maxlayers + 1, new QTableWidgetItem("描述", id));
        }
        _tableWidget->autoResizeCellWidth();
        _tableWidget->setTableItemEditable(false);
    }

    EventOper::OperAbstractSubsystem * BrowserTableHandlerSubsystemSemiInfiniteF::getSubsystemOper()
    {
        //从操作器仓库获取
        return Core::FITKOperatorRepo::getInstance()->
            getOperatorT<EventOper::OperAbstractSubsystem>("OperSubsystemSemiInfiniteFluid");
    }

    bool BrowserTableHandlerSubsystemSemiInfiniteF::enableAttriSet()
    {
        QList<QTableWidgetItem*> selectItems = this->getSelectItems();
        if (selectItems.size() < 2) return false;

        QTableWidgetItem* item = selectItems.at(0);
        if (item == nullptr) return false;
        //名称列
        const int colIndex = item->column();
        //流体材料
        if (colIndex != 2 ) return false;

        return true;
    }
}

